package com.irdstudio.apicenter.console.controller.appmgr;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppBaseInfo;
import com.irdstudio.apicenter.console.dm.appinfo.AppBaseInfoService;
import com.irdstudio.apicenter.console.startup.MacmsRoot;
import com.irdstudio.apicenter.console.util.MacmsPathUtil;
import com.irdstudio.apicenter.core.dvo.RspValue;
import com.irdstudio.apicenter.frame.util.BeanUtils;

/**
 * app工程管理 1.生成基础工程 2.生成模块代码 3.复制工程配置
 * 
 * @author zs.ligm
 * @version 1.0
 * @date 2015-08-06
 */
@Controller
@RequestMapping(value = "/appmgr")
public class AppProjectManager extends MacmsBaseAction {

	/* 基础工程名称 */
	private final static String baseProjectName = "BaseH5ProjectForPhone";

	/* 复制选项-复制配置到已存在的工程 */
	public static String COPY_OPTION_EXIST = "1";

	/* 复制选项-复制配置到新的工程 */
	public static String COPY_OPTION_NEW = "2";

	/**
	 * 进入基础工程准备页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "readyBaseProject")
	public ModelAndView readyBaseProject(HttpServletRequest request)
			throws Exception {

		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数");
		}

		// 取得工程基本信息
		AppBaseInfoService appBaseInfoService = BeanUtils
				.getBean(AppBaseInfoService.class);
		AppBaseInfo appInfo = appBaseInfoService.findById(appId);
		ModelAndView modelAndView = new ModelAndView();
		if (appInfo != null) {
			modelAndView.addObject("appProjectState", MacmsRoot
					.getDataDicService().getValueText("APP_PROJECT_STATE",
							appInfo.getAppProjectState()));
			modelAndView.addObject("AppBaseInfo", appInfo);
		}
		modelAndView.addObject("svnUrl", MacmsRoot.getSvnProjectService()
				.getServerUrl());
		modelAndView.setViewName("/appmgr/readyBaseProject");

		return modelAndView;
	}

	/**
	 * 生成应用基础工程
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "generateBaseProject")
	public @ResponseBody
	ModelAndView generateBaseProject(HttpServletRequest request)
			throws Exception {
		String appId = request.getParameter("appId");

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("ec", "0");
		if (StringUtils.isEmpty(appId)) {
			modelAndView.addObject("ec", "9");
			modelAndView.addObject("em", "必须传入应用标识(appid)参数");
			return modelAndView;
		}

		// 导出基础工程到app工作空间
		String appPrjWs = MacmsPathUtil.getMacmsAppPrjPath(appId);
		if (!MacmsRoot.getSvnProjectService().exportProject(baseProjectName,
				appPrjWs)) {
			modelAndView.addObject("ec", "9");
			modelAndView.addObject("em", "导出工程失败!");
			return modelAndView;
		}

		// 将app工作空间下的工程提交到svn服务器
		RspValue rspValue = MacmsRoot.getSvnProjectService().addProject(appId);

		// 更新数据库信息
		if (rspValue.isOK()) {
			AppBaseInfoService appBaseInfoService = BeanUtils
					.getBean(AppBaseInfoService.class);
			if (!appBaseInfoService.updateAppSvnInfo(appId)) {
				rspValue.resetToError();
				rspValue.setEm("更新应用工程状态及地址信息出错!");
			}
		}

		modelAndView.addObject("ec", rspValue.getEc());
		modelAndView.addObject("em", rspValue.getEm());

		return modelAndView;
	}

	/**
	 * 进入生成模块代码页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "readyProjectModule")
	public String readyProjectModule(HttpServletRequest request)
			throws Exception {
		return "/appmgr/readyProjectModule";
	}

	/**
	 * 生成工程模块代码
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "generateProjectModule")
	public String generateProjectModule(HttpServletRequest request)
			throws Exception {
		return "";
	}

	/**
	 * 进入复制工程配置页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "readyProjectConf")
	public ModelAndView readyProjectConf(HttpServletRequest request)
			throws Exception {

		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数");
		}

		// 取得工程基本信息
		AppBaseInfoService appBaseInfoService = BeanUtils
				.getBean(AppBaseInfoService.class);
		AppBaseInfo appInfo = appBaseInfoService.findById(appId);
		ModelAndView modelAndView = new ModelAndView();
		if (appInfo != null) {
			modelAndView.addObject("appProjectState", MacmsRoot
					.getDataDicService().getValueText("APP_PROJECT_STATE",
							appInfo.getAppProjectState()));
			modelAndView.addObject("AppBaseInfo", appInfo);
		}
		modelAndView.setViewName("/appmgr/readyProjectConf");

		return modelAndView;
	}

	/**
	 * 复制应用配置到另一应用中
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "copyProjectConf")
	public @ResponseBody
	ModelAndView copyProjectConf(HttpServletRequest request) throws Exception {

		String appId = request.getParameter("appId");
		String appName = request.getParameter("appName");
		String srcAppId = request.getParameter("srcAppId");
		String copyOption = request.getParameter("copyOption");

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("ec", "0");
		if (StringUtils.isEmpty(appId)) {
			modelAndView.addObject("ec", "9");
			modelAndView.addObject("em", "必须传入应用标识(appid)参数");
			return modelAndView;
		}
		if (StringUtils.isEmpty(srcAppId)) {
			modelAndView.addObject("ec", "9");
			modelAndView.addObject("em", "必须传入应用标识(srcAppId)参数");
			return modelAndView;
		}
		if (StringUtils.isEmpty(copyOption)) {
			modelAndView.addObject("ec", "9");
			modelAndView.addObject("em", "必须传入应用标识(copyOption)参数");
			return modelAndView;
		}

		// 如果复制配置到一个新的工程中,则先生成一个新的工程的基本信息
		AppProjectManagerDao apmd = new AppProjectManagerDao();
		if (COPY_OPTION_NEW.equals(copyOption)) {
			RspValue rspValue = apmd.createAppBaseInfo(appId, appName);
			if (!rspValue.isOK()) {
				modelAndView.addObject("ec", "9");
				modelAndView.addObject("em", rspValue.getEm());
				return modelAndView;
			}
		}

		String[] appOption = request.getParameterValues("appOption");
		for (int i = 0; i < appOption.length; i++) {
			RspValue rspValue = apmd.copyProjectConf(srcAppId, appId,
					appOption[i]);
			if (!rspValue.isOK()) {
				modelAndView.addObject("ec", "9");
				modelAndView.addObject("em", rspValue.getEm());
				return modelAndView;
			}
		}

		return modelAndView;
	}

	/**
	 * 删除应用配置
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteProjectConf")
	public @ResponseBody
	ModelAndView deleteProjectConf(HttpServletRequest request) throws Exception {

		String appId = request.getParameter("appid");
		String authPasswd = request.getParameter("authPasswd");

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("ec", "0");
		if (StringUtils.isEmpty(appId)) {
			modelAndView.addObject("ec", "9");
			modelAndView.addObject("em", "必须传入应用标识(appid)参数");
			return modelAndView;
		}
		if (StringUtils.isEmpty(authPasswd)
				|| !"deletemacmsapp".equals(authPasswd)) {
			modelAndView.addObject("ec", "9");
			modelAndView.addObject("em", "授权密码不正确!");
			return modelAndView;
		}

		// 删除应用配置
		AppProjectManagerDao apmd = new AppProjectManagerDao();
		RspValue rspValue = apmd.deleteProjectConf(appId);
		if (!rspValue.isOK()) {
			modelAndView.addObject("ec", "9");
			modelAndView.addObject("em", rspValue.getEm());
			return modelAndView;
		}

		return modelAndView;
	}
}
